package gases.as3.librarys.compnents.ui.control
{
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import gases.as3.librarys.compnents.events.GButtonEvent;
	import gases.as3.librarys.compnents.skin.data.BaseSkin;
	import gases.as3.librarys.compnents.skin.data.ButtonSkin;
	import gases.as3.librarys.compnents.ui.GSkinBox;
	import gases.as3.librarys.compnents.ui.GUIComepments;
	
	
	/**
	 * 按钮按下事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="buttonDown", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	/**
	 * 按钮弹起事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="buttonUp", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	/**
	 * 按钮移入事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="buttonOver", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	/**
	 * 按钮移出按下事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="buttonOut", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	/**
	 * 按钮点击事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="buttonClick", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	/**
	 * 按钮选定事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="button_select", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	/**
	 * 按钮解除选定事件
	 * @eventType guwanyuan.as3.librarys.compnents.events.GButtonEvent
	 */	
	[Event(name="button_unselect", type="gases.as3.librarys.compnents.events.GButtonEvent")]
	
	public class GButton extends GUIComepments
	{
		public function GButton()
		{
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.mouseEnabled = true;
			_background = new GSkinBox();
			this.addChild(_background);
			
			addMouseListeners();
		}
		
		override public function set skin(value:BaseSkin):void
		{
			super.skin = value;
			_btnSkin = value as ButtonSkin;
			if(_isSelect && _selectState)
			{
				_background.source = _btnSkin.button_down;
			}
			else
			{
				_background.source = _btnSkin.button_up;
			}
		}
		
		/**
		 * 是否选定后不弹起 
		 * @param value
		 * 
		 */		
		public function set isSelect(value:Boolean):void
		{
			_isSelect = value;
			if(_isSelect)
			{
				checkSelectState();
				if(_selectState && _btnSkin)
				{
					_background.source = _btnSkin.button_down;
				}
			}
		}
		
		/**
		 * 是否选定后不弹起 
		 * @param value
		 * 
		 */	
		public function get isSelect():Boolean
		{
			return _isSelect;
		}
		
		public function set enable(value:Boolean):void
		{
			_enable = value;
			if(_enable)
			{
				addMouseListeners();
			}
			else
			{
				removeMouseListenrs();
			}
		}
		
		public function get enable():Boolean
		{
			return _enable;
		}
		
		/**
		 * 选中状态
		 * @return 
		 * 
		 */		
		public function set selectState(value:Boolean):void
		{
			_selectState = value;
			if(_isSelect)
			{
				checkSelectState();
				if(_selectState && _btnSkin)
				{
					_background.source = _btnSkin.button_down;
				}
				
			}
		}
		
		/**
		 * 选中状态 
		 * @return 
		 * 
		 */		
		public function get selectState():Boolean
		{
			return _selectState;
		}
		
		/**
		 * 获取背景皮肤容器
		 * @return 
		 * 
		 */		
		protected function get background():GSkinBox
		{
			return _background;
		}
		
		
		private function onMouseDown(e:MouseEvent):void
		{
			_background.source = _btnSkin.button_down;
			mouseEventProcess(e, GButtonEvent.BUTTON_DOWN);
			if(_isSelect)
			{
				if(_selectState)
				{
					_selectState = false;
				}
				else
				{
					_selectState = true;
				}
				checkSelectState();
			}
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			_background.source = _btnSkin.button_up;
			mouseEventProcess(e, GButtonEvent.BUTTON_UP);
		}
		
		private function onMouseClick(e:MouseEvent):void
		{
			mouseEventProcess(e, GButtonEvent.BUTTON_CLICK);
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			_background.source = _btnSkin.button_out;
			mouseEventProcess(e, GButtonEvent.BUTTON_OUT);
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			_background.source = _btnSkin.button_over;
			mouseEventProcess(e, GButtonEvent.BUTTON_OVER);
		}
		
		private function onFocusIn(e:FocusEvent):void
		{
			_background.source = _btnSkin.button_over;
			e.stopImmediatePropagation();
		}
		
		private function onFocusOut(e:FocusEvent):void
		{
			_background.source = _btnSkin.button_out;
			e.stopImmediatePropagation();
		}
		
		private function mouseEventProcess(e:MouseEvent, type:String):void
		{
			this.dispatchEvent(new GButtonEvent(type));
			e.stopImmediatePropagation();
			e.updateAfterEvent();
		}
		
		/**
		 * 检测选中状态 
		 * 
		 */		
		private function checkSelectState():void
		{
			//非选中状态
			if(_selectState == false)
			{
				this.addEventListener(MouseEvent.MOUSE_UP, 	  		onMouseUp);
				this.addEventListener(MouseEvent.CLICK, 			onMouseClick);
				this.addEventListener(MouseEvent.ROLL_OUT, 			onMouseOut);
				this.addEventListener(MouseEvent.ROLL_OVER, 		onMouseOver);
				this.addEventListener(FocusEvent.FOCUS_IN, 			onFocusIn);
				this.addEventListener(FocusEvent.FOCUS_OUT, 		onFocusOut);
				this.dispatchEvent(new GButtonEvent(GButtonEvent.BUTTON_UNSELECT));
			}
			else
			{
				this.removeEventListener(MouseEvent.MOUSE_UP, 		onMouseUp);
				this.removeEventListener(MouseEvent.CLICK, 			onMouseClick);
				this.removeEventListener(MouseEvent.ROLL_OUT, 		onMouseOut);
				this.removeEventListener(MouseEvent.ROLL_OVER, 		onMouseOver);
				this.removeEventListener(FocusEvent.FOCUS_IN, 		onFocusIn);
				this.removeEventListener(FocusEvent.FOCUS_OUT, 		onFocusOut);
				this.dispatchEvent(new GButtonEvent(GButtonEvent.BUTTON_SELECT));
			}
		}
		
		private function addMouseListeners():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,  	onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, 	  	onMouseUp);
			this.addEventListener(MouseEvent.CLICK, 		onMouseClick);
			this.addEventListener(MouseEvent.ROLL_OUT, 		onMouseOut);
			this.addEventListener(MouseEvent.ROLL_OVER, 	onMouseOver);
			this.addEventListener(FocusEvent.FOCUS_IN, 		onFocusIn);
			this.addEventListener(FocusEvent.FOCUS_OUT, 	onFocusOut);
		}
		
		private function removeMouseListenrs():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN, 	onMouseDown);
			this.removeEventListener(MouseEvent.MOUSE_UP, 		onMouseUp);
			this.removeEventListener(MouseEvent.CLICK, 			onMouseClick);
			this.removeEventListener(MouseEvent.ROLL_OUT, 		onMouseOut);
			this.removeEventListener(MouseEvent.ROLL_OVER, 		onMouseOver);
			this.removeEventListener(FocusEvent.FOCUS_IN, 		onFocusIn);
			this.removeEventListener(FocusEvent.FOCUS_OUT, 		onFocusOut);
		}
		
		override public function dispose():void
		{
			removeMouseListenrs();
			this.removeChild(_background);
			_background.dispose();
			_background = null;
			_btnSkin = null;
			super.dispose();
		}
		
		private var _btnSkin:ButtonSkin;
		private var _isSelect:Boolean = false;
		private var _selectState:Boolean = false;
		private var _enable:Boolean = true;
		private var _background:GSkinBox;
	}
}


